package rsa

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha512"
)

// RsaSha512Sign 签名
func RsaSha512Sign(data, key string, opts ...string) ([]byte, error) {
	hashed := sha512.Sum512([]byte(data))
	priKey, err := parsePrivateKey(key)
	if err != nil {
		return nil, err
	}

	signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, crypto.SHA512, hashed[:])
	if err != nil {
		return nil, err
	}
	return signature, nil
}

// RsaSha512erySign 验证签名是否正确
func RsaSha512VerySign(data, sign string, pubKey string, opts ...string) (bool, error) {
	publicKey, err1 := parsePublicKey(pubKey)
	if err1 != nil {
		return false, err1
	}

	hashed := sha512.Sum512([]byte(data))

	err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA512, hashed[:], []byte(sign))
	if err != nil {
		return false, err
	}
	return true, nil
}
